package com.ys.controller.web.sys;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bo.dto.NameValueSelectDto;
import com.ys.entity.sys.YsMenu;
import com.ys.enume.sys.YsColEnum;
import com.ys.mapper.sys.SysMapper;
import com.ys.service.sys.YsColService;
import com.ys.vo.W;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 
 * @author Jason
 *
 */
@Controller
@RequestMapping("/web/sys/ysCol")
public class WebYsColController {
	@Autowired
	private SysMapper sysMapper;
	
	@Autowired
	private YsColService ysColService;
	
	/**
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, Long menuId){
		
		List<NameValueSelectDto> typeArr = new ArrayList<NameValueSelectDto>();
		for (YsColEnum.Type e : YsColEnum.Type.values()) {
			typeArr.add(new NameValueSelectDto(e.name, e.value, false));
		}
		
		List<NameValueSelectDto> authTypeArr = new ArrayList<NameValueSelectDto>();
		for (YsColEnum.AuthType e : YsColEnum.AuthType.values()) {
			authTypeArr.add(new NameValueSelectDto(e.name, e.value.toString(), false));
		}
		
		List<NameValueSelectDto> frontTypeArr = new ArrayList<NameValueSelectDto>();
		for (YsColEnum.FrontType e : YsColEnum.FrontType.values()) {
			frontTypeArr.add(new NameValueSelectDto(e.name, e.value, false));
		}
		
		List<NameValueSelectDto> frontConditionTypeArr = new ArrayList<NameValueSelectDto>();
		for (YsColEnum.FrontConditionType e : YsColEnum.FrontConditionType.values()) {
			frontConditionTypeArr.add(new NameValueSelectDto(e.name, e.value, false));
		}
		
		List<NameValueSelectDto> frontConditionRangeTypeArr = new ArrayList<NameValueSelectDto>();
		for (YsColEnum.FrontConditionRangeType e : YsColEnum.FrontConditionRangeType.values()) {
			frontConditionRangeTypeArr.add(new NameValueSelectDto(e.name, e.value, false));
		}
		
		List<NameValueSelectDto> frontFixedArr = new ArrayList<NameValueSelectDto>();
		for (YsColEnum.FrontFixed e : YsColEnum.FrontFixed.values()) {
			frontFixedArr.add(new NameValueSelectDto(e.name, e.value.toString(), e.value == 0));
		}
		
		List<NameValueSelectDto> frontShowArr = new ArrayList<NameValueSelectDto>();
		for (YsColEnum.FrontShow e : YsColEnum.FrontShow.values()) {
			frontShowArr.add(new NameValueSelectDto(e.name, e.value.toString(), e.value == 1));
		}
		
		List<NameValueSelectDto> frontConditionStateArr = new ArrayList<NameValueSelectDto>();
		for (YsColEnum.FrontConditionState e : YsColEnum.FrontConditionState.values()) {
			frontConditionStateArr.add(new NameValueSelectDto(e.name, e.value.toString(), e.value == 0));
		}
		
		List<NameValueSelectDto> frontEditArr = new ArrayList<NameValueSelectDto>();
		for (YsColEnum.FrontEdit e : YsColEnum.FrontEdit.values()) {
			frontEditArr.add(new NameValueSelectDto(e.name, e.value.toString(), e.value == 0));
		}
		
		YsMenu ysMenu = new YsMenu().setId(menuId).queryFirst();
		
		String dataBaseTable = ysMenu.getDataBaseTable();
		String name = ysMenu.getName();
		Integer sortNo = sysMapper.getNextSortNoOfCol(menuId);
		ToAddDto dto = new ToAddDto(menuId, dataBaseTable, name, sortNo, typeArr, authTypeArr, frontTypeArr,
				frontConditionTypeArr, frontConditionRangeTypeArr, frontFixedArr, frontShowArr, frontConditionStateArr, frontEditArr);
		model.addAttribute("dto", dto);
		return "sys/ysCol/toAdd";
	}
	@Data
	@AllArgsConstructor
	private static class ToAddDto{
		private Long menuId; // 模块GUID
		private String dataBaseTable; // 模块唯一标识
		private String name; // 模块名称
		private Integer sortNo; // 前台字段序号
		private List<NameValueSelectDto> typeArr; // 字段类型
		private List<NameValueSelectDto> authTypeArr; // 权限类型
		private List<NameValueSelectDto> frontTypeArr; // 前台控件类型
		private List<NameValueSelectDto> frontConditionTypeArr; // 查询条件类型
		private List<NameValueSelectDto> frontConditionRangeTypeArr; // 查询条件方式
		private List<NameValueSelectDto> frontFixedArr; // 前台是否固定
		private List<NameValueSelectDto> frontShowArr; // 前台是否隐藏
		private List<NameValueSelectDto> frontConditionStateArr; // 前台是否查询
		private List<NameValueSelectDto> frontEditArr; // 前台是否编辑
	}
	
	/**
	 * 选项配置
	 */
	@GetMapping("/toSettingOption")
	public String toSettingOption(Model model, Long colId){
		return ysColService.toSettingOption(model, colId);
	}
	
	/**
	 * 下拉框(多选)配置
	 */
	@GetMapping("/toSettingDropdownMulty")
	public String toSettingDropdownMulty(Model model, Long colId){
		return ysColService.toSettingDropdownMulty(model, colId);
	}
	
	/**
	 * 表单配置
	 */
	@GetMapping("/toSettingForm")
	public String toSettingForm(Model model, Long menuId){
		return W.error(model, "功能暂未开放");
//		return ysColService.toSettingForm(model, menuId);
	}
	
	/**
	 * 弹出框配置
	 */
	@GetMapping("/toSettingSelectorModal")
	public String toSettingSelectorModal(Model model, Long colId){
		return ysColService.toSettingSelectorModal(model, colId);
	}
	
	/**
	 * 设置多文件控件的类型
	 */
	@GetMapping("/toSettingMultyFileSourceType")
	public String toSettingMultyFileSourceType(Model model, Long colId){
		return ysColService.toSettingMultyFileSourceType(model, colId);
	}
}
